# -*- coding:utf8 -*-
import os
from flask import Flask
from config import config
from ext import db
# 导入蓝图
from app.controller.task import todo_v1_bp


def create_app(config_name, config_path=None):
    """工厂函数"""
    app = Flask(__name__)
    # 设置为False应该可以解决中文乱码问题。
    app.config['JSON_AS_ASCII'] = False
    # 识别配置文件
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    # 注册扩展
    register_extensions(app)
    # 注册蓝图
    register_blueprints(app)
    return app


def register_extensions(app):
    """注册扩展"""
    # 数据库注册
    db.init_app(app=app)


def register_blueprints(app):
    """注册蓝图"""
    # task
    app.register_blueprint(todo_v1_bp)
